package dao;

import entity.News;
import entity.Video;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;
import utils.HibernateUtils;

import java.util.List;

public class NewsDaoImpl implements NewsDao{
    //按type获取总数
    @Override
    public Long newsCount(int type) {
        Session session = HibernateUtils.getSession();
        Query query=session.createQuery("select count(*) from entity.News as n where n.type =:type and isDelete = 0");
        query.setParameter("type",type);
        return (Long) query.uniqueResult();
    }
    //分页
    @Override
    public List<News> selectNewsType(int type, int pageSize, int pageNum,int state) {
        Session session = HibernateUtils.getSession();
        Query query = session.createQuery("from entity.News v1 where v1.type =:type and v1.state =:state and isDelete = 0 order by v1.newsId desc");
        query.setParameter("type",type);
        query.setParameter("state",state);
        query.setFirstResult(pageNum);
        query.setMaxResults(pageSize);
        return (List<News>) query.list();
    }

    @Override
    public int createNews(News news) {
        Session session = null;
        Transaction tx = null;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            session.save(news);
            tx.commit();
            return 1;
        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
            return 0;
        }finally {
            HibernateUtils.closeSession();
        }
    }

    @Override
    public News selectByNewsId(String newsId) {
        Session session = HibernateUtils.getSession();
        Query query=session.createQuery("from entity.News n where n.newsId =:newsId");
        query.setParameter("newsId",newsId);
        return (News) query.uniqueResult();
    }

    @Override
    public List<News> selectNewsByUid(int uid,int state) {
        Session session = HibernateUtils.getSession();
        Query query = session.createQuery("from entity.News v1 where v1.userId =:uid and v1.state =:state order by v1.newsId desc");
        query.setParameter("uid",uid);
        query.setParameter("state",state);
        return (List<News>) query.list();
    }

    @Override
    public int deleteNews(String newsId) {
        HibernateUtils.rebuildSessionFactory();
        Session session = null;
        Transaction tx = null;
        int count = 0;
        try {
            session = HibernateUtils.getSession();
            tx = session.beginTransaction();
            Query query = session.createQuery("update entity.News as n set n.isDelete = ? where n.newsId =:newsId");
            query.setParameter(0,1);
            query.setParameter("newsId",newsId);
            count = query.executeUpdate();
            tx.commit();
        }catch (Exception e){
            tx.rollback();
            e.printStackTrace();
            return 0;
        }finally {
            HibernateUtils.closeSession();
            return count;
        }
    }
}
